﻿using RoleSelection;
using RoleSelection.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.ResourceManager;
using WindowsFormsApplication1.Utils;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            f = this;
        }

        private static Form1 f = null;
        private string stFilePath = string.Empty;
        private void button1_Click(object sender, EventArgs e)
        {
            if (!IsLogin)
            {
                MessageBox.Show("您还没有登录，请先登录");
                return;
            }
            var config = AreaConfiguration.Instance;
            var areas = config.AreaInfos;
            int tjrs = 0;//统计角色总数
            for (int i = 0; i < areas.Count; i++)
            {
                var currentArea = areas[i];
                //大区名称
                var regionName = currentArea.T;
                foreach (var option in currentArea.Options)
                {
                    //服务器名称
                    var serverName = option.T;
                    string msg = string.Format("当前正在查询{0}区服务器：{1},请稍候。。。", regionName, serverName);
                    this.label1.Text = msg;

                    var area = option.V;

                    string cookie = loginFm.webBrowser1.Document.Cookie;

                    string respBody = HttpHelper.Get("http://comm.aci.game.qq.com/main?game=qqhx&area=" + area + "&sCloudApiName=ams.gameattr.role&callback=156790507346517188&_=1567909758513",
                        cookie, "http://qqhx.qq.com/cp/a20140515hxglxt/page02.shtml");

                    var body = RespHelper.GetRespInfo(respBody);
                    
                    if (!body.DataDict.ContainsKey(RespDataConstants.ErrorInfo) || 
                        string.IsNullOrEmpty(body.DataDict[RespDataConstants.ErrorInfo]))
                    {
                        ListViewItem listViewItem = new ListViewItem();
                        //listViewItem.SubItems.Add(regionName + "-" + serverName);
                        listViewItem.Text = regionName + " " + serverName;

                        var roleInfo = System.Web.HttpUtility.UrlDecode(body.DataDict[RespDataConstants.List]);
                       
                        listViewItem.SubItems.Add(roleInfo);

                        tjrs+=Convert.ToInt32(roleInfo[0].ToString());
                       
                       
                        this.listView1.Items.Add(listViewItem);
                        
                    }
                }
            }
           
            MessageBox.Show("所有区服数据已获取完毕！总计："+tjrs.ToString()+"个角色");
            this.label1.Text = "所有区服数据已获取完毕！";
        }

        private static bool _isLogin;
        public static bool IsLogin {
            get
            {
                return _isLogin;
            }
            set {
                _isLogin = value;
                if (_isLogin)
                {
                    f.linkLabel1.Text = "您已经登录！";
                }
            }
        }

        Login loginFm = new Login();
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (IsLogin)
            {
                MessageBox.Show("已经登录过了，请不要重复登录！");
            }else
            {
                loginFm.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            
            stFilePath = dir + @"\QQ华夏全服角色一览 " + DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss").Replace(":","") + ".txt";
           
            StreamWriter swStream;
            if (File.Exists(stFilePath))
            {
                swStream = new StreamWriter(stFilePath); 
            }
            else
            {
                swStream = File.CreateText(stFilePath);
            }

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                for (int j = 0; j < listView1.Items[i].SubItems.Count; j++)
                {
                    string _strTemp = listView1.Items[i].SubItems[j].Text;
                    swStream.Write(_strTemp);
                    
                    swStream.Write("\t");
                }
                swStream.WriteLine();
            }

           
            swStream.Flush();
            swStream.Close();
           
            Process.Start("notepad.exe", stFilePath);
        }
    }


}
